(function(){
	var dataCache = Ccay.DataCache = {
		//创建数据缓存
		create:function(op){
			return new dataCache.Cache(op);
		},
		//创建数据缓存集合
		createCaches:function(op){
			return new dataCache.CacheCollection(op);
		}
	};
	
    dataCache.Cache = $.IObject({
    	init:function(op){
    		$.extend(this,op);
    		this.clear();
    	},
    	//生成缓存主键
    	buildKey:function(data){
    		var arr = [];
    		for(var i = 0,len = this.keys.length;i < len;i++){
    			var key = this.keys[i],value;
    			if(!key)
    				return;
    				
    			value = Ccay.DataHelper.getObj(key,data);
    			if(value == null) 
    				return;
				arr.push(value);
    		}
    		return arr.join(this.split || "-");
    	},
    	//清空缓存
    	clear:function(){
    		this._store = {};
    		this.length = 0;
    	},
    	//循环缓存项
    	each:function(fnHandle){
    		if(this.length){
    			$.each(this._store,function(i,item){
    				fnHandle(item,i);
    			});
    		}
    		return this;
    	},
    	//缓存数据/卸载
    	cache:function(data,discharge){
    		var key = this.buildKey(data),value = this.getValue(key);
    		if(discharge)
    		{
    			delete this._store[key];
    			value != null && this.length--;
    		}
    		else
    		{
    			if(!this.checkLimit())
    				return false;
    			if(arguments.length > 2)
    			{
	    			var args = Array.prototype.slice.call(arguments,0);
	    			args[1] = key;
	    			this._store[key] = this.buildValue.apply(this,args);
    			}
    			else
    				this._store[key] = this.buildValue(data,key);
    				
    			value == null && this.length++;
    		}
    		return true;
    	},
    	checkLimit:function(){
    		if(this.limitCount){
    			if(this.length + 1 > this.limitCount){
	    			this.fnOverLimit && this.fnOverLimit.call(this,this.length,this.limitCount);
	    			return false;
    			}
    		}
    		return true;
    	},
    	getValue:function(name, isHasKey){
    		if(typeof(name) != "string")
    			name = this.buildKey(name);
    		return isHasKey ? {key: name, value: this._store[name]} : this._store[name];
    	},
    	//生成缓存的值
    	buildValue:function(data){
    		return data;
    	},
    	//合并缓存的值
    	merge:function(data){
    		var oriValue = this.getValue(data, true);
    		if(oriValue.value != null){
    			this._store[oriValue.key] = $.extend(data, oriValue.value);
    			return true;
    		}
    	},
    	//将缓存项转换成数组
    	toArray:function(){
    		var arr = [];
    		if(this.length){
	    		this.each(function(item,key){
	    			arr.push(item);
	    		})
    		}
    		return arr;
    	}
    });
    
    dataCache.CacheCollection = $.IObject({
    	init:function(op){
    		$.extend(this,op);
    		this.Caches = {};
    		var self = this;
    		$.each(op.caches,function(name,cacheOp){
    			op.caches[name] = self.initCache(name,cacheOp);
    		});
    		this.limitCount;// || (this.limitCount = 100);
    		delete this.caches;
    	},
    	//初始化缓存单项
    	initCache:function(name,cacheOp){
    		cacheOp.keys || (cacheOp.keys = this.keys);
    		this.Caches[name] = new dataCache.Cache(cacheOp); 
    	},
    	//清除所有缓存
    	clear:function(){
    		$.each(this.Caches,function(i,cache){
    			cache.clear();
    		});
    	},
    	//清除单项缓存
    	clearCache:function(name){
    		this.getCache(name).clear();
    	},
    	//获取单项缓存对象
    	getCache:function(name){
    		return this.Caches[name];
    	},
    	//判断是否超过限制的大小
    	checkLimit:function(){
    		if(this.limitCount){
    			var count = this.count();
    			if(count + 1 > this.limitCount){
    				//触发超出范围事件
	    			this.fnOverLimit && this.fnOverLimit.call(this,count,this.limitCount);
	    			return false;
    			}
    		}
    		return true;
    	},
    	//缓存数据/卸载（针对单项缓存）
    	cache:function(name,data,discharge){
    		if(!discharge && !this.checkLimit())
    			return false;
    		var cache = this.getCache(name);
    		cache.cache.apply(cache,Array.prototype.slice.call(arguments,1));
    		return true;
    	},
    	//缓存项的总数
    	count:function(){
    		var count = 0;
    		$.each(this.Caches,function(i,cache){
    			count += cache.length;
    		})
    		return count;
    	},
    	//缓存数据合并方法
    	merge:function(datas){
			var caches = [];
    		$.each(this.Caches,function(i,cache){
    			if(cache.length)
    				caches.push(cache);
    		});
    		if(caches.length)
    		{
	    		var self = this;
	    		$.each(datas,function(i,item){
	    			self.mergeItem(item,caches);
	    		});
    		}
    	},
    	//单项缓存数据合并方法
    	mergeItem:function(item,caches){
    		$.each(caches,function(i,cache){
    			if(cache.merge(item))
    				return false;
    		})
    	},
    	//将缓存转换成对象树组
    	toArray:function(){
    		var result = {};
    		$.each(this.Caches,function(i,cache){
    			result[i] = cache.toArray();
    		});
    		return result;
    	}
    });    
	
})();